//
//  ScanningViewController.m
//  Go!
//
//  Created by rimi on 16/7/19.
//  Copyright © 2016年 凌风. All rights reserved.
//

#import "ScanningViewController.h"
#import "ZBarSDK.h"

@interface ScanningViewController ()<ZBarReaderViewDelegate,UIAlertViewDelegate>
{
    ZBarReaderView *_readerView;
    UIImageView *_line;
    NSTimer *_timer;
    int num;
    BOOL upOrdown;
    
    NSString *_string;
    BOOL _main;
}
@end

@implementation ScanningViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    _readerView = [[ZBarReaderView alloc] init]; /**< 初始化扫描视图 */
    _readerView.frame = CGRectMake(0, 0, 250, 250); /**< 初始化扫描区域 */
    _readerView.center = self.view.center;
    _readerView.readerDelegate = self;
    
    //关闭闪光灯
    _readerView.torchMode = 0;
    
    //扫描区域
    CGRect scanMaskRect = CGRectMake(0, 0, 250, 250);
    
    if (TARGET_IPHONE_SIMULATOR) {
        ZBarCameraSimulator *cameraSimulator = [[ZBarCameraSimulator alloc] initWithViewController:self];
        cameraSimulator.readerView = _readerView;
    }
    [self.view addSubview:_readerView];
    
    _line = [[UIImageView alloc] initWithFrame:CGRectMake(20, 0, 210, 2)];
    _line.image = [UIImage imageNamed:@"line.png"];
    [_readerView addSubview:_line];
    _timer = [NSTimer scheduledTimerWithTimeInterval:.02 target:self selector:@selector(animation1) userInfo:nil repeats:YES];
    
    
    //   扫描区域计算
    _readerView.scanCrop = [self getScanCropWithScanRect:scanMaskRect andReaderViewBounds:_readerView.bounds];
    _readerView.scanCrop = CGRectMake(0.1, 0.2, 0.8, 0.8);//扫描的感应框
    
    [_readerView start];
    
    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, _readerView.frame.origin.y + _readerView.frame.size.height + 50, 320, 20)];
    label1.text = @"将二维码放入框内，即可自动扫描";
    label1.font = [UIFont systemFontOfSize:16];
    label1.textAlignment = NSTextAlignmentCenter;
    label1.backgroundColor = [UIColor clearColor];
    [self.view addSubview:label1];
    
    
    // Do any additional setup after loading the view.
}


- (CGRect)getScanCropWithScanRect:(CGRect)rect andReaderViewBounds:(CGRect)rvBounds
{
    CGFloat x,y,width,height;
    
    x = rect.origin.y / rvBounds.size.height;
    y = 1 - (rect.origin.x + rect.size.width) / rvBounds.size.width;
    width = rect.size.height / rvBounds.size.height;
    height = rect.size.width / rvBounds.size.width;
    
    return CGRectMake(x, y, width, height);
    
}

- (void)readerView:(ZBarReaderView *)readerView didReadSymbols:(ZBarSymbolSet *)symbols fromImage:(UIImage *)image
{
    for (ZBarSymbol *symbol in symbols) {
        if ([symbol.data hasPrefix:@"code:"]) {
            _string = [NSString stringWithString:[symbol.data substringFromIndex:5]];
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"扫描成功，是否消费？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            [alertView show];
            [_readerView stop];
            [_timer invalidate];
            _line.alpha = 0.0;
            num = 0;
            upOrdown = NO;
        }else{
            
            if ([symbol.data hasPrefix:@"http://"]) {
                [[UIApplication sharedApplication]openURL:[NSURL URLWithString:symbol.data]];
                
            }else{
                [[UIApplication sharedApplication]openURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://%@",symbol.data]]];
                
            }
        }
        break;
    }
}

-(void)animation1
{
    if (upOrdown == NO) {
        num ++;
        _line.frame = CGRectMake(20, 2*num, 210, 2);
        if (2*num == 250) {
            upOrdown = YES;
        }
    }
    else {
        num --;
        _line.frame = CGRectMake(20, 2*num, 210, 2);
        if (num == 0) {
            upOrdown = NO;
        }
    }
    
    
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 1) {
        
    }else{
        [_readerView start];
        _timer = [NSTimer scheduledTimerWithTimeInterval:.02 target:self selector:@selector(animation1) userInfo:nil repeats:YES];
        _line.alpha = 1.0;
    }
}


@end
